package com.code2roc.fastboot.framework.extra.systemload;

import com.code2roc.fastboot.framework.cache.CacheUtil;
import com.code2roc.fastboot.framework.database.MapperBeanManage;
import com.code2roc.fastboot.framework.extra.datadic.DataDicManage;
import com.code2roc.fastboot.framework.extra.jsonhandle.FastJsonUtil;
import com.code2roc.fastboot.framework.config.SystemConfig;
import com.code2roc.fastboot.framework.quartz.QuartzUtil;
import com.code2roc.fastboot.framework.util.*;
import com.mysql.cj.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.Map;

@Component
@Order(value = 10)
@DependsOn("flywayInitializer")
public class StartUpContext implements ApplicationRunner {
    private static Logger log = LoggerFactory.getLogger(StartUpContext.class);
    @Autowired
    private ApplicationContext context;
    @Autowired
    private MapperBeanManage mapperBeanManage;
    @Autowired
    private DataDicManage dataDicManage;
    @Autowired
    private CacheUtil cacheUtil;
    @Autowired
    private QuartzUtil quartzUtil;
    @Autowired
    private SystemConfig systemConfig;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        Date start = new Date();
        log.debug("fastboot开始启动");

        SystemLoaderUtil.clearLoaderTag();

        Map<String, Object> annotatedBeans = context.getBeansWithAnnotation(SpringBootApplication.class);
        Class bootClass = annotatedBeans.values().toArray()[0].getClass();

        //清除静态文件
        ClassPathFileUtil.clearClassPathFileTemplateFolder();
        //扫描数据字典
        dataDicManage.initCodeItem(bootClass);
        //扫描dao
        mapperBeanManage.initMapperBean(bootClass);
        //扫描定时任务
        quartzUtil.initQuartzJob(bootClass);
        //初始化缓存
        cacheUtil.initCache(bootClass);
        //jsonfield个性序列化时间格式
        FastJsonUtil.scanDate2Json(bootClass);

        //执行自定义系统启动事件
        Map<String, ISystemStartExecute> res = context.getBeansOfType(ISystemStartExecute.class);
        for (Map.Entry en : res.entrySet()) {
            ISystemStartExecute service = (ISystemStartExecute) en.getValue();
            service.execute();
        }

        SystemLoaderUtil.updateFrameVersion();
        SystemLoaderUtil.addLoaderTag();

        long loadPeroid = DateUtil.getTimePeroidToNowMillSecond(start);
        String version = SystemLoaderUtil.getFrameVersion();
        log.debug("【框架版本号】：" + version);
        log.debug("【启动耗时】：" + loadPeroid + "毫秒");
        Environment environment = BeanUtil.getBean(Environment.class);
        String serverPort = environment.getProperty("server.port");
        String systemTag = environment.getProperty("system.systemTag");
        String contextpath = environment.getProperty("server.servlet.context-path");
        if(StringUtils.isNullOrEmpty(contextpath) ){
            contextpath="";
        }
        log.debug("【访问地址】：" + "http://localhost:" +   serverPort +contextpath + "/" + systemTag + "/login.html");
    }
}
